package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.constants.MqConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @GetMapping("/list")
    public PageDTO<Item> itemPageInfo(@RequestParam(name = "page",defaultValue = "1") Integer page,@RequestParam(name = "size",defaultValue = "5") Integer size) {
      log.info("页码:{},条数:{}",page,size);
        Page<Item> itemPage = new Page<>(page, size);
        itemService.page(itemPage);
        List<Item> records = itemPage.getRecords();
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        itemPageDTO.setList(records);
        itemPageDTO.setTotal(Long.valueOf(itemService.count()));
        return itemPageDTO;
    }

    @GetMapping("/{id}")
    public Item getItemById(@PathVariable Integer id) {
        return itemService.getById(id);
    }

    @PostMapping
    public void postItem(@RequestBody Item item) {
        itemService.save(item);
        log.info("successfully added product data!{}",item);
    }

    @PutMapping("/status/{id}/{status}")
    public void putItemState(@PathVariable Long id,@PathVariable int status) {
        log.info("打印参数id:{},status:{}",id,status);
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemService.updateById(item);
        log.info("successfully modified product ->{} status -> {}..",id,status);
        if (status == 1) {
            //商品上架 写入新增消息队列中给mq消费
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_INSERT_KEY,id);
        } else {
            //商品下架 同样写入删除队列中删除对应文档id数据
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_DELETE_KEY,id);
        }
    }

    @PutMapping
    public void updateItemInfo(@RequestBody Item item) {
        itemService.updateById(item);
        log.info("修改后的数据:{}.",item);
    }

    @DeleteMapping("/{id}")
    public void deleteItemInfo(@PathVariable Long id) {
        itemService.removeById(id);
        log.info("successfully deleted data with id {}",id);
    }

    @GetMapping("/getItem/{id}")
    public Item getItemById(@PathVariable(name = "id") Long id) {
        return itemService.getById(id);
    }

    @GetMapping("/stock/{itemId}/{num}")
    public void useStockData(@PathVariable Long itemId,@PathVariable Integer num) {
        Item item = itemService.getById(itemId);
        int stockNow = item.getStock() - num;
        if (stockNow < 0) throw new RuntimeException("error: less than zero [库存数异常]");
        item.setStock(stockNow);
        itemService.updateById(item);
    }
}
